package net.cathackers.devel.scmp.chat.impl;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

import org.apache.log4j.Logger;

import net.cathackers.devel.scmp.chat.interfaces.ChatConfiguration;
import net.cathackers.devel.scmp.factories.SCMPFactory;

public class ChatConfigurationImpl implements ChatConfiguration {
    private Properties configBackend_;
    private Logger	logger_;
    protected ChatConfigurationImpl() {
	logger_ = SCMPFactory.getLogger(this);
    }
    public ChatConfigurationImpl(String filename) {
	this();
	configBackend_ = new Properties(new DefaultProperties());
	if (filename != null) {
	    try {
		configBackend_.loadFromXML(new FileInputStream(filename));
	    } catch (IOException ex) {
		//log this error and we automatically fallback to defaults.
		logger_.warn("Couldn't load configuration file " + filename + ", falling back to defaults...");
	    }
	}
    }

    @Override
    public int getChatServerPort() {
	return Integer.parseInt(configBackend_.getProperty("ChatServerPort"));
    }
    
    @Override
    public int getSecureChatServerPort() {
	return Integer.parseInt(configBackend_.getProperty("ChatSecureServerPort"));
    }

    @Override
    public String getChatServerDomain() {
	return configBackend_.getProperty("ChatServerDomain");
    }

    public void setChatServerDomain(String serverName) {
	configBackend_.setProperty("ChatServerDomain", serverName);
    }
    
    @Override
    public String getGroupChatServerDomain() {
	return configBackend_.getProperty("GroupChatServerDomain");
    }
    
    public class DefaultProperties extends Properties {

	private static final long serialVersionUID = 1399415041392968318L;

	public DefaultProperties() {
	    this.setProperty("ChatServerPort", "5222");
	    this.setProperty("ChatServerIP", "0.0.0.0");
	    this.setProperty("ChatServerDomain", "cat-hackers.net");
	    this.setProperty("GroupChatServerDomain", "conference.cat-hackers.net");

	}
    }

    @Override
    public String getChatServerIP() {
	return configBackend_.getProperty("ChatServerIP");
    }
    
    public void setChatServerIP(String ip) {
	configBackend_.setProperty("ChatServerIP", ip);
    }

}
